package input.list;

import array.Array;
import input.ParameterConverter;

import java.util.StringJoiner;

public class ListNodeConverter implements ParameterConverter {

    @Override
    public Object convert(String param) {
        Array array = new Array(param);
        if (array.length == 0) {
            return null;
        }
        ListNode dummy = new ListNode();
        ListNode cur = dummy;
        for (int i = 0; i < array.length; i++) {
            cur.next = new ListNode(array.get(i).asInt());
            cur = cur.next;
        }
        return dummy.next;
    }

    public static String listNodeToString(ListNode head) {
        StringJoiner strJoiner = new StringJoiner(",", "[", "]");
        while (head != null) {
            strJoiner.add(String.valueOf(head.val));
            head = head.next;
        }
        return strJoiner.toString();
    }
}
